import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:kxy_flutter_app/model/test.dart';
import 'package:kxy_flutter_app/r.dart';
import 'package:kxy_flutter_app/view/examination/do_test_view.dart';

class SimpleTestWidget extends StatelessWidget {
  final List<Test> testList;
  final Function refresh;

  const SimpleTestWidget({
    Key key,
    @required this.testList,
    @required this.refresh,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: ListTile.divideTiles(
        tiles: testList.map((test) {
          return Container(
            color: Colors.white,
            child: examItemWidget(test, context),
          );
        }),
        context: context,
        color: Colors.black12,
      ).toList(),
    );
  }

  Widget examItemWidget(Test test, BuildContext context) {
    var format = new DateFormat('yyyy年MM月dd日');
    return ListTile(
      leading: scroeWidget(test.ratio),
      title: Text(
        test.name,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(
          fontSize: 16,
          color: Colors.black,
        ),
      ),
      subtitle: Text(
        test.publishDate == null ? '' : format.format(test.publishDate),
        style: TextStyle(
          fontSize: 12,
          color: Colors.black54,
        ),
      ),
      trailing: btnDoTest(test, context),
    );
  }

  Widget scroeWidget(double input) {
    int ratio = (input * 100).toInt();
    if (ratio < 0) {
      return Image.asset(
        R.assetsExamNoAnswer,
        height: 40,
        width: 40,
        fit: BoxFit.cover,
      );
    }

    if (ratio > 60) {
      return Container(
        alignment: Alignment.center,
        decoration: BoxDecoration(
          color: Color.fromRGBO(102, 211, 159, 1),
          shape: BoxShape.circle,
          border: Border.all(width: 1, color: Color.fromRGBO(102, 211, 159, 1)),
        ),
        height: 40,
        width: 40,
        child: Text(
          '$ratio%',
          style: TextStyle(
            fontSize: 12,
            color: Colors.white,
          ),
        ),
      );
    }

    return Container(
      alignment: Alignment.center,
      decoration: BoxDecoration(
        color: Color.fromRGBO(255, 142, 128, 1),
        shape: BoxShape.circle,
      ),
      height: 40,
      width: 40,
      child: Text(
        '$ratio%',
        style: TextStyle(
          fontSize: 12,
          color: Colors.white,
        ),
      ),
    );
  }

  Widget btnDoTest(Test test, BuildContext context) {
    if (test.joinExam) {
      return GestureDetector(
        child: Container(
          padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(50),
            color: Theme.of(context).primaryColor,
          ),
          child: Text(
            '去答题',
            style: TextStyle(
              fontSize: 12,
              color: Colors.white,
            ),
          ),
        ),
        onTap: () async {
          await Navigator.of(context).push(
            MaterialPageRoute(
              builder: (cxt) => DoTestView(test: test),
            ),
          );
          refresh();
        },
      );
    }

    return Container(
      padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(50),
        color: Color.fromRGBO(245, 245, 245, 1),
        border: Border.all(width: 1, color: Color.fromRGBO(230, 230, 230, 1)),
      ),
      child: Text(
        '去答题',
        style: TextStyle(
          fontSize: 12,
          color: Colors.black54,
        ),
      ),
    );
  }
}
